package com.ktgj.other.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.common.core.utils.SnowFlake;
import com.ktgj.common.core.utils.SnowflakeUtils;
import com.ktgj.common.core.utils.StringUtils;
import com.ktgj.other.domain.common.*;
import com.ktgj.other.utils.GaoDeUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RentCarImpl implements RentCar {
    /**
     * 正式环境请求地址
     */
    //String apiUrl = "http://api.car.jryghq.com/";
    /**
     * 测试环境请求地址
     */
    String apiUrl = "http://beta.car.jryghq.com/";

    /**
     * 正式环境密钥
     */
    //String SecretKey = "f9brwaif";
    /**
     * 测试环境密钥
     */
    String SecretKey = "12345678";

    /**
     * 正式环境合作商编号
     */
    int companyId = 41;
    /**
     * 测试环境合作商编号
     */
    //int companyId = 45;

    @Autowired
    private GaoDeUtil gaoDeUtil;

    //价格信息查询
    @Override
    public DayPriceResponse getDayPrice(DayPriceRequest request) {
        request.companyId = companyId;
        String sign = getSign(request);
        request.sign = sign;
        JSONObject object = (JSONObject) JSONObject.toJSON(request);
        object.remove("carTypeId");
        String json = JSONObject.toJSONString(object);
        String postJson = HttpClientUtil.doPostJson(apiUrl + "Car/Price", json);
        DayPriceResponse response = JSON.parseObject(postJson, DayPriceResponse.class);
        if (response.statusCode.equals("SUCCESS"))
            response.IsSucess = true;
        return response;
    }

    //下单接口
    @Override
    public CreateOrderResponse createOrder(CreateOrderRequest request) {
        request.companyId = companyId;
        /*SnowflakeUtils snowflake = new SnowflakeUtils();
        long id = snowflake.nextId();
        request.vendorOrderCode = String.valueOf(id);*/
        String sign = getSign(request);
        request.sign = sign;
        String json = JSONObject.toJSONString(request);
        String postJson = HttpClientUtil.doPostJson(apiUrl + "Car/CreateOrder", json);
        CreateOrderResponse response = JSON.parseObject(postJson, CreateOrderResponse.class);
        if (response.statusCode.equals("SUCCESS"))
            response.IsSucess = true;
        return response;
    }

    //取消金额
    @Override
    public OrderCancelFeeResponse orderCancelFee(OrderCancelFeeRequest request) throws Exception {
        if (!StringUtils.isNotEmpty(request.vendorOrderCode))
            throw new Exception("平台订单编号不能为空！");
        request.companyId = companyId;
        String sign = getSign(request);
        request.sign = sign;
        String json = JSONObject.toJSONString(request);
        String postJson = HttpClientUtil.doPostJson(apiUrl + "Car/OrderCancelFee", json);
        OrderCancelFeeResponse response = JSON.parseObject(postJson, OrderCancelFeeResponse.class);
        if (response.code == 1)
            response.IsSucess = true;
        return response;
    }

    //订单取消
    @Override
    public CancelOrderResponse cancelOrderConfirm(CancelOrderConfirmRequest request) {
        request.companyId = companyId;
        String sign = getSign(request);
        request.sign = sign;
        String json = JSONObject.toJSONString(request);
        String postJson = HttpClientUtil.doPostJson(apiUrl + "Car/CancelOrderConfirm", json);
        CancelOrderResponse response = JSON.parseObject(postJson, CancelOrderResponse.class);
        if (response.statusCode.equals("SUCCESS"))
            response.IsSucess = true;
        return response;
    }

    //订单查询
    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) throws Exception {
        if (!StringUtils.isNotEmpty(request.orderCode))
            throw new Exception("阳光车导订单号不能为空！");
        if (!StringUtils.isNotEmpty(request.vendorOrderCode))
            throw new Exception("平台订单编号不能为空！");
        request.companyId = companyId;
        String sign = getSign(request);
        request.sign = sign;
        String json = JSONObject.toJSONString(request);
        String postJson = HttpClientUtil.doPostJson(apiUrl + "Car/OrderDetail", json);
        OrderDetailResponse response = JSON.parseObject(postJson, OrderDetailResponse.class);
        return response;
    }

    //订单实时位置
    @Override
    public OrderPositionResponse orderPosition(OrderPositionRequest request) throws Exception {
        if (!StringUtils.isNotEmpty(request.vendorOrderCode))
            throw new Exception("平台订单编号不能为空！");
        request.companyId = companyId;
        String sign = getSign(request);
        request.sign = sign;
        String json = JSONObject.toJSONString(request);
        String postJson = HttpClientUtil.doPostJson(apiUrl + "Car/OrderPosition", json);
        OrderPositionResponse response = JSON.parseObject(postJson, OrderPositionResponse.class);
        return response;
    }

    //根据地址获取经纬度
    @Override
    public Map<String, Object> getLatAndLngByAddress(String address) {
        Map<String, Object> latAndLngByAddress = gaoDeUtil.getLatAndLngByAddress(address);
        return latAndLngByAddress;
    }

    /**
     * 签名
     * @param request
     * @return
     */
    private String getSign(Object request) {
        String md5 = null;
        String json = JSONObject.toJSONString(request);
        SortedMap sortedMap = JSONObject.parseObject(json, SortedMap.class);
        StringBuffer buffer = new StringBuffer();
        Set<?> set = sortedMap.entrySet();
        Iterator<?> iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (!key.toString().equals("sign") && !key.toString().equals("carTypeId")) {
                if (value != null && value != "") {
                    if (key.toString().equals("carTypeList")) {
                        List<Integer> carTypeList = (List<Integer>) value;
                        if (carTypeList != null && carTypeList.size() > 0) {
                            String carType = "";
                            for (Integer integer : carTypeList) {
                                carType += integer.toString() + ",";
                            }
                            buffer.append(key.toString() + carType.substring(0, carType.length() - 1));
                        } else {
                            buffer.append(key.toString());
                        }
                    } else {
                        buffer.append(key.toString() + value.toString());
                    }

                } else {
                    buffer.append(key.toString());
                }
            }
        }
        buffer.append(SecretKey);
        md5 = DigestUtils.md5Hex(buffer.toString()).toUpperCase();
        return md5;
    }
}
